package com.js.data.system.controller;


import com.js.data.common.api.CommonResult;
import com.js.data.system.annotation.Log;
import com.js.data.system.dto.DeptDTO;
import com.js.data.system.vo.CommonTreeVO;
import com.js.data.system.vo.SimpleUserVO;
import com.js.data.system.service.DeptService;
import com.js.data.system.vo.DeptTreeVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author wujj
 * @since 2022-09-06
 */

@RestController
@RequestMapping("/sys")
public class DeptController {

    @Resource
    private DeptService deptService;


    /**
     * 组织机构树
     *
     * @param parentId 父id,默认为0
     * @return OrganizeTreeVO
     * @author wujj
     * @since 2020/6/5 17:06
     */
    @PreAuthorize("@perm.check('system:dept:tree')")
    @GetMapping(value = "/dept/{parentId}/tree", produces = "application/json; charset=utf-8")
    public CommonResult<List<DeptTreeVO>> treeDept(@PathVariable Long parentId) {
        return CommonResult.success(deptService.treeDept(parentId));
    }

    /**
     * 新增部门
     *
     * @param deptDTO 部门表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:dept:add')")
    @Log(value = "新增部门")
    @PostMapping(value = "/dept")
    public CommonResult<Void> saveDept(@Validated @RequestBody DeptDTO deptDTO) {
        deptService.saveDept(deptDTO);
        return CommonResult.success();
    }

    /**
     * 修改部门
     *
     * @param id      部门id
     * @param deptDTO 部门表单
     * @return void
     */
    @PreAuthorize("@perm.check('system:dept:edit')")
    @Log(value = "修改部门")
    @PutMapping(value = "/dept/{id}")
    public CommonResult<Void> editDept(@PathVariable Long id, @Validated @RequestBody DeptDTO deptDTO) {
        deptService.editDept(id, deptDTO);
        return CommonResult.success();
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @return void
     */
    @PreAuthorize("@perm.check('system:dept:del')")
    @Log(value = "删除部门")
    @DeleteMapping(value = "/dept/{id}")
    public CommonResult<Void> deleteDept(@PathVariable Long id) {
        deptService.deleteDept(id);
        return CommonResult.success();
    }


    /**
     * 简单组织机构树
     *
     * @param parentId 父id,默认为0
     * @param orgType  组织机构类型,数据字典key(org_type)
     * @return DepTreeVO
     * @author wujj
     * @since 2020/6/5 17:06
     */
    @GetMapping(value = "/dept/simple/{parentId}", produces = "application/json; charset=utf-8")
    public CommonResult<List<CommonTreeVO>> treeSimpleDept(@PathVariable Long parentId,
                                                           @RequestParam(value = "orgType", required = false) String orgType) {
        return CommonResult.success(deptService.treeSimpleDept(parentId, orgType));
    }

    /**
     * 查询部门下所有的用户（包括子部门的用户）
     *
     * @param deptId 部门id
     * @return List<SimpleUserVO>
     */
    @GetMapping(value = "/dept/{deptId}/user", produces = "application/json; charset=utf-8")
    public CommonResult<List<SimpleUserVO>> listAllUserByDept(@PathVariable Long deptId) {
        return CommonResult.success(deptService.listAllUserByDept(deptId));
    }

}
